package net.interest.curation;

import java.util.ArrayList;
import java.util.List;

import net.interest.curation.meta.FacebookUserMeta;
import net.interest.curation.meta.RssMeta;
import net.interest.curation.meta.TwitterUserMeta;
import net.interest.curation.model.FacebookUser;
import net.interest.curation.model.Rss;
import net.interest.curation.model.SocialUser;
import net.interest.curation.model.TwitterUser;
import net.interest.curation.model.User;

import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import scenic3.ScenicPage;
import scenic3.annotation.ActionPath;
import scenic3.annotation.Default;
import scenic3.annotation.Page;
import scenic3.annotation.Var;

@Page("/")
public class FrontPage extends ScenicPage {
    // /view/100  /view/200
    @ActionPath("view/{id}")
    public Navigation view(@Var("id") String id) {
        super.request.setAttribute("id", id);
        return forward("/view.jsp");
    }

    // /
    @Default
    public Navigation index() {
        User user = (User) super.request.getSession().getAttribute(User.SESSION_USER_ATTR_NAME);
        List<SocialUser> accountList = new ArrayList<SocialUser>();
        TwitterUserMeta meta = TwitterUserMeta.get();
        List<TwitterUser> twusers = Datastore.query(meta).filter(meta.userRef.equal(user.getKey())).asList();
        accountList.addAll(twusers);
        FacebookUserMeta fbMeta = FacebookUserMeta.get();
        List<FacebookUser> fbUsers = Datastore.query(fbMeta).filter(fbMeta.userRef.equal(user.getKey())).asList();
        accountList.addAll( fbUsers) ;
        

        RssMeta rssmeta = RssMeta.get();
        List<Rss> rsses = Datastore.query(rssmeta).filter(meta.userRef.equal(user.getKey())).asList();
        
        request.setAttribute("accounts", accountList);
        request.setAttribute("rsses", rsses);
        request.setAttribute("rssSize", rsses.size());
        return forward("/index.jsp");
    }
}